# from PreTest import *
from pyecharts import options as opts
from pyecharts.render import make_snapshot
from snapshot_phantomjs import snapshot

from pyecharts.charts import Gauge
datas = []
total = 0.00000
def shuju():
    # -*-coding: utf-8 -*-
    import pymysql

    db = pymysql.connect(host="localhost", user="root", passwd="123456", database="pythondata", charset="utf8")
    print('服务器连接成功!')

    sql = 'select zhejiu from test'

    cursor = db.cursor()

    cursor.execute(sql)
    global  datas
    datas = list(cursor.fetchall())

    print(datas)

def qiuzhi():
    global datas
    global total
    # print(type(datas))
    # datas = list(datas)
    # print(type(datas))
    for ele in range(0, len(datas)):
        total = total + float(datas[ele])
    print(total)


def gauge_chart() -> Gauge:
    # ********* Begin *********#
    gauge = (
        Gauge()
        .add("业务指标",[("完成率", 55.5)],radius="50%",split_number=5,
        axisline_opts=opts.AxisLineOpts(
            linestyle_opts=opts.LineStyleOpts(
                color=[(0.3, "#67e0e3"), (0.7, "#37a2da"), (1, "#fd666d")], width=30)
        ),
        detail_label_opts=opts.LabelOpts(formatter="{value}")
    )
    .set_global_opts(
        title_opts=opts.TitleOpts(title="Gauge-分割段数-Label"),
        legend_opts=opts.LegendOpts(is_show=False))
    )
    return gauge
def test():
    import pandas as pd
    from sqlalchemy import create_engine
    # 初始化数据库连接，使用pymysql模块
    # MySQL的用户：root, 密码:147369, 端口：3306,数据库：test
    engine = create_engine('mysql+pymysql://root:123456@localhost:3306/pythondata')
    # 查询语句，选出employee表中的所有数据
    sql = ''' select zhejiu from test; '''
    # read_sql_query的两个参数: sql语句， 数据库连接
    df = pd.read_sql_query(sql, engine)
    # 输出employee表的查询结果
    print(type(df))
    junzhi = df.mean()
    minOne = df.min()
    maxOne = df.max()
    print(junzhi)
    print(minOne)
    print(maxOne)

    # 新建pandas中的DataFrame, 只有id,num两列
    # df = pd.DataFrame({'id': [1, 2, 3, 4], 'name': ['zhangsan', 'lisi', 'wangwu', 'zhuliu']})
    # 将新建的DataFrame储存为MySQL中的数据表，储存index列
    # df.to_sql('mydf', engine, index=True)
    # print('Read from and write to Mysql table successfully!')

test()
# shuju()
# qiuzhi()
# make_snapshot(snapshot, gauge_chart().render("仪表图.html"), 'student_answer.png') # 输出图片
